//
// Created by 崔世强 on 2018/1/30.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import UIKit

class Common {
    static func getLocalUrl(_ fileName:String, file:String) -> URL {
        let fm = FileManager.default
        let thumbnailUrlForDocument = fm.urls(for: .documentDirectory, in:.userDomainMask)[0] as URL   //documents文件夹
        let thumbnailUrlForEmotionUrl = thumbnailUrlForDocument.appendingPathComponent(file, isDirectory: true)

        //创建文件地址
        let emotionToThumbnailUrl = thumbnailUrlForEmotionUrl.appendingPathComponent("\(fileName)", isDirectory: false)
        return emotionToThumbnailUrl
    }
    
    static func videourl(_ fileName:String) -> URL? {
        return URL(string:"\(IM_HTTP_SERVER_URL_STRING)im/file/download/videos/\(fileName)")
    }
    
    static func stickerUrl(_ fileName:String) -> URL? {
        return URL(string:"\(IM_HTTP_SERVER_URL_STRING)im/file/download/stickers/\(fileName)")
    }
    
    
    static func imgUrl(_ fileName:String) -> URL? {
//        return URL(string:"https://waim-resource.oss-cn-beijing.aliyuncs.com/im/img/\(fileName)?x-oss-process=style/im-img-thumbnail")
        return URL(string: "\(IM_HTTP_SERVER_URL_STRING)im/file/download/imgs/\(fileName)")
    }
    static func imgOrigUrl(_ fileName:String) -> URL? {
//        return URL(string: "https://waim-resource.oss-cn-beijing.aliyuncs.com/im/img/\(fileName)")
        return URL(string: "\(IM_HTTP_SERVER_URL_STRING)im/file/download/imgs/\(fileName)")
    }
    
    static func avatarUrl(_ avatarName:String) -> URL? {
//        return URL(string: "https://waim-resource.oss-cn-beijing.aliyuncs.com/im/img/\(avatarName)")
        return URL(string: "\(IM_HTTP_SERVER_URL_STRING)im/file/download/imgs/\(avatarName)")
    }
    
    static func fileUrl(_ fileName:String) -> URL? {
        return URL(string:"\(IM_HTTP_SERVER_URL_STRING)im/file/download/files/\(fileName)")
    }
    
    static func getSizeString(size: Int) -> String {
        let size:CGFloat      = CGFloat(size)
        let sizeUnit: CGFloat = 1024.0
        if size < sizeUnit { // KB
            return String(format: "%.1fKB", size)
        } else if size < sizeUnit * sizeUnit {  // MB
            return String(format: "%.1fMB", size / sizeUnit)
        } else {   // GB
            return String(format: "%.1fG", size / (sizeUnit * sizeUnit))
        }
    }
    
    static func getImgSize(_ origSize:CGSize) -> CGSize {
        let minSize = CGSize(width: 60, height: 60)
        let maxSize = CGSize(width: 120, height: 120)
        let minValue = CGFloat(60)
        let maxValue = CGFloat(120)
        if (origSize.width >= minSize.width && origSize.height >= minSize.height) && (origSize.width <= maxSize.width && origSize.height <= maxSize.height) {
            return origSize
        }
        let widthHeightRatio = origSize.width/origSize.height  //计算宽高比
        if widthHeightRatio <= 4 || widthHeightRatio >= 0.25 {
            let ratio = max(origSize.width, origSize.height)/maxValue
            return CGSize(width: origSize.width/ratio, height: origSize.height/ratio)
        } else {
            let ratio = max(origSize.width, origSize.height)/minValue
            return CGSize(width: origSize.width/ratio, height: origSize.height/ratio)
        }
    }
}

// MARK:- 颜色方法
func normalRGBA (r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) -> UIColor {
    return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
func RGBA (r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat) -> UIColor {
    return UIColor (red: r, green: g, blue: b, alpha: a)
}

// MARK:- 常用按钮颜色
let kBtnWhite = RGBA(r: 0.97, g: 0.97, b: 0.97, a: 1.00)
let kBtnDisabledWhite = RGBA(r: 0.97, g: 0.97, b: 0.97, a: 0.30)
let kBtnGreen = RGBA(r: 0.15, g: 0.67, b: 0.16, a: 1.00)
let kBtnDisabledGreen = RGBA(r: 0.65, g: 0.87, b: 0.65, a: 1.00)
let kBtnRed = RGBA(r: 0.89, g: 0.27, b: 0.27, a: 1.00)
// 分割线颜色
let kSplitLineColor = RGBA(r: 0.78, g: 0.78, b: 0.80, a: 1.00)
// 常规背景颜色
let kCommonBgColor = RGBA(r: 0.92, g: 0.92, b: 0.92, a: 1.00)
let kSectionColor = RGBA(r: 0.94, g: 0.94, b: 0.96, a: 1.00)
// 导航栏背景颜色
let kNavBarBgColor = normalRGBA(r: 20, g: 20, b: 20, a: 0.9)

// 表情键盘颜色大全
let kChatBoxColor = normalRGBA(r: 244.0, g: 244.0, b: 246.0, a: 1.0)
let kLineGrayColor = normalRGBA(r: 188.0, g: 188.0, b: 188.0, a: 0.6)

//聊天输入框文字大小
let kInputViewFontSize:CGFloat = 17.5

let sensitiveFileName = "sensitive.txt"
let sensitiveFileVersion = "sensitiveFileVersion"

// 屏幕宽度
let kScreenH = UIScreen.main.bounds.height
// 屏幕高度
let kScreenW = UIScreen.main.bounds.width

/* ============================== 语音界面提示文字 ============================== */
let recordBtnTextTalk = "按住 说话"
let recordBtnTextStop = "松开 结束"
let recordViewNoteLabelCancel = "松开手指，取消发送"
let recordViewNoteLabelNormal = "手指上滑，取消发送"
let recordViewNoteLabelTooShort = "说话时间太短"
let textLengthTooLong = "消息不能超过500字"

/* ============================== 录音按钮长按事件 ============================== */
let proximityStateDidChange = "UIDeviceProximityStateDidChangeNotification"
let recordBtnLongTapBegan = "RecordBtnLongTapBegan"
let recordBtnLongTapChanged = "RecordBtnLongTapChanged"
let recordBtnLongTapEnded = "RecordBtnLongTapEnded"
let newMsg = "onNewMsg"

let noteChatMsgAudioPlayBegin = "noteChatMsgAudioPlayBegin"
let noteChatMsgAudioPlayEnd = "noteChatMsgAudioPlayEnd"
let noteChatMsgAudioPlayInterrupted = "noteChatMsgAudioPlayInterrupted"

let kNoteChatMsgTapImg = "noteChatMsgTapImg" //点击消息图片
let kNoteChatMsgTapWebURL = "noteChatMsgTapWebURL" // 点击web页面
let kNoteChatMsgGroupMute = "noteChatMsgGroupMute" // 全体禁言/解禁
let kNoteChatMsgGroupMemberChange = "kNoteChatMsgGroupMemberChange" // 被添加进群/被踢出群
let kNoteChatMsgGroupChangeName = "noteChatMsgGroupChangeName" //修改群名称
let kNoteChatMsgGroupChangeAnnouncement = "noteChatMsgGroupChangeAnnouncement" //修改群公告


let noteChatRoomNewMsg = "noteChatRoomNewMsg" //聊天室新消息
let remindedGroupMem = "remindedGroupMem"  //被提醒的群成员




let connectionStatusChange =  Notification.Name("onConnectionStatusChange")
let addRowNotiName =  Notification.Name("addRowNotiName")
let closeCurrentVCNoti =  Notification.Name("closeCurrentVCNoti")
let playAndRecordNoti = Notification.Name("playAndRecordNoti") //听筒播放
let playBackNoti = Notification.Name("playBackNoti") // 扬声器播放
let refreshSingleOrGroupVCNoti =  Notification.Name("refreshSingleOrGroupVC")
